package com.atom.kotlin.app

import android.Manifest
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.ScrollView
import com.atom.module.logger.Logger
import com.atom.module.permissions.AutoPermissions
import com.atom.module.permissions.AutoPermissionsCallback
import com.atom.module.picker.FileSelectCallBack
import com.atom.module.picker.FileSelectResult
import com.atom.module.picker.FileSelector
import com.atom.module.picker.FileType
import java.lang.StringBuilder

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createContentView())
    }

    private fun createContentView(): View {
        val contentView = FrameLayout(this)
        val scrollView = ScrollView(this)
        scrollView.layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        val linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        linearLayout.post {
            buttons.forEach { entry ->
                linearLayout.addView(Button(this).also {
                    it.text = entry
                    it.setOnClickListener(MenuActivity@ this)
                })
            }
        }
        scrollView.addView(linearLayout)
        contentView.addView(scrollView)
        return contentView
    }

    val buttons = arrayListOf(
        "CodeView",
        "单选文件",
        "多选文件",
        "单选VIDEO",
        "多选VIDEO",
    )

    override fun onClick(v: View?) {
        if (v is Button) {
            when (v.text) {
                "单选文件" -> {
                    AutoPermissions.loadSelectedPermission(
                        this,
                        1024,
                        Manifest.permission_group.STORAGE,
                        object : AutoPermissionsCallback {
                            override fun onDenied(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                            }

                            override fun onGranted(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                                FileSelector.apply {
                                    this.setRequestCode(4096)
                                    this.setMultiSelect(false)
                                }.pick(this@MainActivity , object : FileSelectCallBack{
                                    override fun onSuccess(results: List<FileSelectResult>) {
                                        Logger.e(results.first().string(this@MainActivity))
                                    }

                                    override fun onError(e: Throwable) {
                                        Logger.e(e)
                                    }
                                })
                            }
                        })
                }
                "多选文件" -> {
                    AutoPermissions.loadSelectedPermission(
                        this,
                        1024,
                        Manifest.permission_group.STORAGE,
                        object : AutoPermissionsCallback {
                            override fun onDenied(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                            }

                            override fun onGranted(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                                FileSelector.apply {
                                    this.setRequestCode(4096)
                                    this.setMultiSelect(true)
                                }.pick(this@MainActivity , object : FileSelectCallBack{
                                    override fun onSuccess(results: List<FileSelectResult>) {
                                        val string = StringBuilder()
                                        results.forEach {
                                            string.append(it.string(this@MainActivity))
                                            string.append("\n")
                                        }
                                        Logger.e(string.toString())
                                    }

                                    override fun onError(e: Throwable) {
                                        Logger.e(e)
                                    }
                                })
                            }
                        })
                }
                "单选VIDEO" -> {
                    AutoPermissions.loadSelectedPermission(
                        this,
                        1024,
                        Manifest.permission_group.STORAGE,
                        object : AutoPermissionsCallback {
                            override fun onDenied(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                            }

                            override fun onGranted(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                                FileSelector.apply {
                                    this.setRequestCode(4096)
                                    this.setMultiSelect(false)
                                    this.setMimeType(FileType.VIDEO.getMimeType())
                                }.pick(this@MainActivity , object : FileSelectCallBack{
                                    override fun onSuccess(results: List<FileSelectResult>) {
                                        Logger.e(results.first().string(this@MainActivity))
                                    }

                                    override fun onError(e: Throwable) {
                                        Logger.e(e)
                                    }
                                })
                            }
                        })
                }
                "多选VIDEO" -> {
                    AutoPermissions.loadSelectedPermission(
                        this,
                        1024,
                        Manifest.permission_group.STORAGE,
                        object : AutoPermissionsCallback {
                            override fun onDenied(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                            }

                            override fun onGranted(requestCode: Int, permissions: Array<String>) {
                                Logger.e(requestCode, *permissions)
                                FileSelector.apply {
                                    this.setRequestCode(4096)
                                    this.setMultiSelect(true)
                                    this.setMimeType(FileType.VIDEO.getMimeType())
                                }.pick(this@MainActivity , object : FileSelectCallBack{
                                    override fun onSuccess(results: List<FileSelectResult>) {
                                        val string = StringBuilder()
                                        results.forEach {
                                            string.append(it.string(this@MainActivity))
                                            string.append("\n")
                                        }
                                        Logger.e(string.toString())
                                    }

                                    override fun onError(e: Throwable) {
                                        Logger.e(e)
                                    }
                                })
                            }
                        })
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        FileSelector.parseResult(this, requestCode, resultCode, data)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        AutoPermissions.parsePermissions(this, requestCode, permissions)
    }
}